package com.neusoft.databus.common.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.validator.constraints.Length;

import com.neusoft.bizcore.web.jpa.model.AbstractAuditModel;
import com.neusoft.databus.common.enums.MetricUsageType;
import com.neusoft.databus.common.enums.Protocol;

import lombok.Getter;
import lombok.Setter;

/**
 * 指标组 <br>
 * 指标的逻辑分组（协议、周期、用途），保证指标的复用性
 *
 * @author naxiang
 */
@Getter
@Setter
@Entity
//@TypeDefs({ @TypeDef(name = "StringJsonObject", typeClass = StringJsonUserType.class) })
public class MetricGroup extends AbstractAuditModel {

    private static final long serialVersionUID = 6257132642655558736L;

    /**
     * 指标组编码
     */
    @NotBlank(message = "指标组编码不能为空")
    @Length(max = 255, message = "指标组编码不能超过{max}位")
    @Column(length = 255, nullable = false, unique = true)
    private String code;

    /**
     * 指标组名称(变量名)
     */
    @NotBlank(message = "指标组变量名不能为空")
    @Length(max = 255, message = "指标组变量名不能超过{max}位")
    @Column(length = 255, nullable = false)
    private String name;

    /**
     * 指标组协议（解析指标用)
     */
    @Enumerated(EnumType.STRING)
    @NotNull(message = "指标组协议不能为空")
    @Column(length = 30, nullable = false)
    private Protocol protocol;

    /**
     * 扩展配置 <br />
     * 在用户自定义协议时，使用config保存配置信息
     */
    @Length(max = 1023, message = "扩展配置不能超过{max}位")
    @Column(length = 1023)
    private String config;

    //    /**
    //     * 采集周期(任务调度用)
    //     */
    //    @NotNull(message = "采集周期不能为空")
    //    @Min(value = 1, message = "采集周期不能小于{value}秒")
    //    @Column(nullable = false)
    //    private Long executionCycle;// 指标组的采集周期

    /**
     * 指标组用途（暂时只支持采集)
     */
    @Enumerated(EnumType.STRING)
    @Column(length = 30, nullable = false)
    private MetricUsageType usageType = MetricUsageType.COLLECT;

    /**
     * 关联指标。维护关系方
     */
    @ManyToMany(targetEntity = Metric.class, cascade = {CascadeType.MERGE, CascadeType.PERSIST })
    @JoinTable(name = "ref_metric_group", joinColumns = {
            @JoinColumn(name = "metric_group_id", referencedColumnName = "id") }, inverseJoinColumns = {
                    @JoinColumn(name = "metric_id", referencedColumnName = "id") })
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<Metric> metrics = new ArrayList<>();

    /**
     * 关联资源。关系在Resource中维护
     */
    @ManyToMany(targetEntity = Resource.class)
    @JoinTable(name = "ref_resource_metric_group", joinColumns = {
            @JoinColumn(name = "metric_group_id", referencedColumnName = "id") }, inverseJoinColumns = {
                    @JoinColumn(name = "resouce_id", referencedColumnName = "id") })
    @LazyCollection(LazyCollectionOption.TRUE)
    private List<Resource> resources = new ArrayList<>();

    /** 注释 */
    @Length(max = 1023, message = "注释不能超过{max}位")
    @Column(length = 1023, nullable = true)
    private String comment;
}
